package com.breakfast.fun.ui.user.login;

import android.util.Log;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.Login;
import com.breakfast.fun.data.entity.user.Result;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.EncyptUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.io.IOException;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by hp on 2016/8/23.
 */
public class LoginPresenter extends LoginContract.Presenter {


    @Override
    public void onStart() {

    }

    @Override
    public void loginByPwd(String mobile, String pwd) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "login");
        map.put("mobile", mobile);
        map.put("password", pwd);

        showLoadingDialog(true);

        rxManager.add(model.getLoginByPwdModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Login>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Login login) {
                        hideLoadingDialog();
                        if (login.isSuccess()) {
                            view.loginSuccess(login.getData());
                            getRecommendCode();
                        } else {
                            T.show(login.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void loginByCode(String mobile, String code) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "quickMobileLogin");
        map.put("mobile", mobile);
        map.put("code", code);
        String sign = null;
        try {
            sign = EncyptUtil.getSignature(map, Constant.appSecret);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("sign", sign);

        showLoadingDialog(true);

        rxManager.add(model.getLoginByPwdModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Login>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Login login) {
                        hideLoadingDialog();
                        if (login.isSuccess()) {
                            view.loginSuccess(login.getData());
                            getRecommendCode();
                            //第一次手机号登录返回‘注册成功’，弹出大礼包
                            if (login.getMsg().contains("注册")) {
                                view.openGiftActivity();
                            }
                        } else {
                            T.show(login.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void getRecommendCode() {
        Map<String, String> map = Api.getInstance().getBaseParams();
        rxManager.add(model.getRecommendCodeModel(map).subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        if (result.isSuccess()) {
                            view.saveRecommendCode(result.getData().toString());
                        }
                    }

                }));
    }

    @Override
    public void getLoginCode(String mobile) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "quickMobileCode");
        map.put("mobile", mobile);

        showLoadingDialog(true);

        rxManager.add(model.getLoginCodeModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("发送失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        hideLoadingDialog();
                        if (result.isSuccess()) {
                            view.sendCodeSuccess();
                        } else {
                            T.show(result.getMsg());
                        }
                    }

                }));
    }
}
